<?php
namespace V1\Model;
/**
 * Description of newPHPClass
 *
 * @property   $name Description
 * 
 * @author wangzhenxian
 * @package application.components 
 */
use Think\Model;
class GameDetailModel extends Model {
    
  
    /**
     * 获取一个游戏的猜涨跌和极值游戏的信息
     * @param  string  $username 用户名
     * @param  string  $password 用户密码
     * @return integer           登录成功-用户ID，登录失败-错误编号
     */
    public function getGamesInfoById($game_id){
            $map = array(
                'game_id'=>$game_id,
//                'begin_time'=>array('elt',NOW_TIME),
//                'end_time'=>array('egt',NOW_TIME),
                'status' => 1,
            );
            /* 获取用户数据 */
            $all_total_scores = $this->where($map)->sum('total_scores');
            $all_up_scores = $this->where($map)->sum('up_scores');
            //$all_down_scores = $this->where($map)->sum('down_scores');
            $all_joins = $this->where($map)->sum('joins');
            
            $up_ratio=  number_format($all_up_scores/$all_total_scores,2);
            $down_ratio=1-$up_ratio;
            
            return array('all_total_scores'=>$all_total_scores,'all_joins'=>$all_joins,'up_ratio'=>$up_ratio,'down_ratio'=>$down_ratio);
    }
    
    public function getGamesInfoById2($game_id){
            $map = array(
                'game_id'=>$game_id,
//                'status' => 1,
            );
            /* 获取用户数据 */
            $all_total_scores = $this->where($map)->cache(60)->sum('total_scores');
            $all_joins = $this->where($map)->cache(60)->sum('joins');
            
            return array('all_joins'=>$all_joins,'all_total_money'=>$all_total_scores);
    }
    
    
    public function getGameInfoBySymbol($game_id,$symbol){
            
            $map = array(
                'game_id'=>$game_id,
                'symbol'=>$symbol,
//                'begin_time'=>array('elt',NOW_TIME),
//                'end_time'=>array('egt',NOW_TIME),
//                'status' => 1,
            );
            
            $fields=['game_id','symbol','total_scores','up_scores','down_scores','joins','begin_time','end_time'];
            $gameInfo = $this->where($map)->field($fields)->find();
            
            if(!empty($gameInfo)){
                $gameInfo['up_ratio']=  number_format($gameInfo['up_scores']/$gameInfo['total_scores'],2);
                $gameInfo['down_ratio']=1-$gameInfo['up_ratio'];
                $gameInfo['left_time'] = $gameInfo['end_time'] - NOW_TIME;
            }
            
            return $gameInfo;
    }
      public function getOverGameInfoBySymbol($game_id,$symbol){
            
            $map = array(
                'game_id'=>$game_id,
                'symbol'=>$symbol,
                'status' => 2,
            );
            
            $fields=['game_id','symbol','total_scores','up_scores','down_scores','joins','begin_time','end_time'];
            
            $gameInfo = $this->where($map)->fields($fields)->find();
            
            if(!empty($gameInfo)){
                $gameInfo['up_ratio']=  number_format($gameInfo['up_scores']/$gameInfo['total_scores'],2);
                $gameInfo['down_ratio']=1- $gameInfo['up_ratio'];
            }
            
            return $gameInfo;
    }
     
    /**
     * 更新游戏及局状态
     * @param type $game_id
     * @param type $gulstatus
     * @param type $symbol
     * @param type $gamestatus
     */
    public function updateGameStatus($game_id,$gulstatus,$symbol='',$gamestatus=0) {
       
       $where=['game_id'=>$game_id,];
       !empty($symbol) && $where['symbol'] = $symbol;
        
        $this->where($where)->setField('status', $gulstatus);   
        
        if(!empty($gamestatus)){
            M('Game')->where('id='.$game_id)->setField('status', $gamestatus);
        }
    }
    public  function getLastGulGame($game_id){
        
        $where=['game_id'=>$game_id,'symbol'=>array('notlike','%0'),'status'=>1];
        $result = $this->where($where)->order('id desc')->field('symbol')->find();
        if(empty($result)) return 0;
        return intval(substr($result['symbol'], -1, 1));
    }
    /**
     * 获取游戏局状态
     * @param type $game_id
     * @param type $symbol
     * @return type
     */
    public function getGameStatus($game_id,$symbol){
        
        $curGameStatus = S($symbol,'',array('prefix'=>'gamestatus'));
        
        if(empty($curGameStatus)){
            $where=['game_id'=>$game_id,'symbol'=>$symbol];
            $curGameStatus = $this->where($where)->getField('status');
            S($symbol,$curGameStatus,array('expire'=>'72000','prefix'=>'gamestatus'));
        }
        return $curGameStatus;
    }
    /**
     * 生成游戏信息
     * @param type $day
     * @param type $stock_code
     */
    public function mkGamesGul($day,$stockcode){
        
        $stockcode = getStockWithFlag($stockcode);
        $stockInfo = M('StockInfo')->where("state=1 AND stockcode='{$stockcode}'")->find();
     
        $gameData=array('stock_id'=>$stockInfo['id'],'stock_code'=>$stockInfo['stockcode'],'stock_name'=>$stockInfo['stockname'],
            'stock_logo'=>"",'default_bets'=>1,'max_multipe'=>10,'join_limit'=>0,'fee'=>0,
            'trading_day'=>$day,'create_time'=>NOW_TIME,'status'=>1);
        
        $gameModel = M('Game');
        
        if($gameModel->create($gameData)){
            $game_id = $gameModel->add();
            if($game_id){
               $gulGameTimes=[
                ['begin_time'=>'07:00','end_time'=>'13:00','prize_time'=>'15:00','balance_time'=>'16:00'],
                ['begin_time'=>'09:00','end_time'=>'10:00','prize_time'=>'10:30','balance_time'=>'10:45'],
                ['begin_time'=>'10:00','end_time'=>'11:00','prize_time'=>'11:30','balance_time'=>'11:45'],
                ['begin_time'=>'12:30','end_time'=>'13:30','prize_time'=>'14:00','balance_time'=>'14:15'],
                ['begin_time'=>'13:30','end_time'=>'14:30','prize_time'=>'15:00','balance_time'=>'15:15'],
            ];
           $gulGamesData=[];
            foreach ($gulGameTimes as $key => $value) {
                $symbol = date('Ymd',strtotime($day)).substr($stockcode, 2).$key;
                $gulGamesData[]=array(
                    'game_id'=>$game_id,'symbol'=>$symbol,
                    'begin_time'=>strtotime("{$day} {$value['begin_time']}"),
                    'end_time'=>strtotime("{$day} {$value['end_time']}"),
                    'prize_time'=>strtotime("{$day} {$value['prize_time']}"),
                    'balance_time'=>strtotime("{$day} {$value['balance_time']}"),
                    'status'=>1,
                ); 
            }
            
           $this->addAll($gulGamesData);
            
        }
        
    }else{
        echo $gameModel->getError();
    }
}
    /**
     * 用户生成订单后 修改局相关概要信息
     * @param type $symbol 游戏唯一标识符
     * @param type $game_type 游戏类型 猜涨跌 gul 或涨跌幅 extremun
     * @param type $guess_result 游戏结果 猜涨跌 猜涨2或跌1  涨跌幅：-10~10
     * @param type $scores
     */
    public function addOrder($symbol,$game_type,$guess_result,$scores){

        $this->where("symbol='{$symbol}'")->setInc('total_scores',$scores);
        $this->where("symbol='{$symbol}'")->setInc('joins',1);
        
        if(($guess_result==2 && $game_type==='gul')||($guess_result > 0 && $game_type==='extremun')){
            
            $this->where("symbol='{$symbol}'")->setInc('up_scores',$scores);
            $this->where("symbol='{$symbol}'")->setInc('join_up',1);
            
        }else if(($guess_result==1 && $game_type==='gul')||($guess_result < 0 && $game_type==='extremun')){
            
            $this->where("symbol='{$symbol}'")->setInc('down_scores',$scores);
            $this->where("symbol='{$symbol}'")->setInc('join_down',1);
            
        }

    }

}
